package com.wuxiaoqin.server.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaoqin.server.service.SearchService;
import com.wuxiaoqin.server.vo.GoodsVo;

import com.wuxiaoqin.uitls.pageUtil.ShopPageInfo;
import com.wuxiaoqin.uitls.util.Decimal;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 搜索
     * @param searchStr
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<GoodsVo> doSearch(String searchStr, Integer pageNum, Integer pageSize) {
        try {
            //指定索引库
            SearchRequest searchRequest = new SearchRequest("sixteenclothes");
            //构建查询对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //设置分页条件
            searchSourceBuilder.from((pageNum-1)*pageSize).size(pageSize);
            //构建高亮对象
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            //设置高亮字节及高亮样式
            highlightBuilder.field("goodsName")
                    .preTags("<font color=\"#FF0000\">")
                    .postTags("</font>");
            searchSourceBuilder.highlighter(highlightBuilder);
            //添加查询条件
            searchSourceBuilder.query(QueryBuilders.multiMatchQuery(searchStr,"goodsName"));
            searchRequest.source(searchSourceBuilder);
            //执行请求
            List<GoodsVo> list = new ArrayList<>();
            SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
            //处理数据
                //总条数
            Long total = search.getHits().getTotalHits().value;
            if (0>total){
                return null;
            }
            SearchHit[] hits = search.getHits().getHits();
            for (SearchHit hit : hits) {
                Integer goodsId = Integer.valueOf((Integer) hit.getSourceAsMap().get("goodsId"));
                String goodsName = String.valueOf(hit.getSourceAsMap().get("goodsName"));
                String highlightMessage = String.valueOf(hit.getHighlightFields().get("goodsName").fragments()[0]);
                BigDecimal marketPrice = new BigDecimal(String.valueOf(hit.getSourceAsMap().get("marketPrice")));
                String originalImg = String.valueOf(hit.getSourceAsMap().get("originalImg"));
                String goodsDescribe = String.valueOf(hit.getSourceAsMap().get("goodsDescribe"));
                GoodsVo goodsVo = new GoodsVo(goodsId, goodsName, highlightMessage, marketPrice, originalImg, goodsDescribe);
                list.add(goodsVo);
            }
            Page<GoodsVo> goodsVoPage = new Page<>(pageNum, pageSize, total.intValue());
            //ShopPageInfo<GoodsVo> objectShopPageInfo = new ShopPageInfo<GoodsVo>(pageNum,pageSize,total.intValue());
            goodsVoPage.setRecords(list);
            return goodsVoPage;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Page<GoodsVo> selectByCondition(Integer pn, Integer size, BigDecimal maxPrice, BigDecimal minPrice,String searchStr) {
        try {
            //指定索引库
            SearchRequest searchRequest = new SearchRequest("sixteenclothes");
            //构建查询对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //设置分页条件
            searchSourceBuilder.from((pn-1)*size).size(size);
            //构建高亮对象
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            //设置高亮字节及高亮样式
            highlightBuilder.field("goodsName")
                    .preTags("<font color=\"#FF0000\">")
                    .postTags("</font>");
            searchSourceBuilder.highlighter(highlightBuilder);
            //添加查询条件
            searchSourceBuilder.query(QueryBuilders.multiMatchQuery(searchStr,"goodsName"));
            searchRequest.source(searchSourceBuilder);
            //执行请求
            List<GoodsVo> list = new ArrayList<>();
            SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
            //处理数据
            //总条数
            Long total = search.getHits().getTotalHits().value;
            if (0>total){
                return null;
            }
            SearchHit[] hits = search.getHits().getHits();
            for (SearchHit hit : hits) {
                Integer goodsId = Integer.valueOf((Integer) hit.getSourceAsMap().get("goodsId"));
                String goodsName = String.valueOf(hit.getSourceAsMap().get("goodsName"));
                String highlightMessage = String.valueOf(hit.getHighlightFields().get("goodsName").fragments()[0]);
                BigDecimal marketPrice = new BigDecimal(String.valueOf(hit.getSourceAsMap().get("marketPrice")));
                String originalImg = String.valueOf(hit.getSourceAsMap().get("originalImg"));
                String goodsDescribe = String.valueOf(hit.getSourceAsMap().get("goodsDescribe"));
                Decimal decimal = new Decimal(marketPrice);
                if (decimal.ge(minPrice)&&decimal.le(maxPrice)) {
                    GoodsVo goodsVo = new GoodsVo(goodsId, goodsName, highlightMessage, marketPrice, originalImg, goodsDescribe);
                    list.add(goodsVo);
                }
            }
            Page<GoodsVo> goodsVoPage = new Page<>(pn, size, total);
            goodsVoPage.setRecords(list);
            return goodsVoPage;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
